var express = require('express')
var dateformat = require('dateformat')
var render = require('../lib/renderFile')
var renderYnu = require('../lib/renderFileYNU')

var Company = require('../model/Company')
var YNUCompany = require('../model/YNUCompany')

var router = express.Router()
// 按照时间
router.get('/', function(req, res, next) {
  const filter = req.filter
  if (filter.school === 'kmust') {
    console.log(req.filter)
    Company.count({}, (err, count) => {
      Company.find(filter.filter)
        .sort(filter.sort)
        .limit(filter.limit)
        .skip((filter.page - 1) * filter.limit)
        .exec((err, doc) => {
          if (filter.file) {
            render(doc, res)
          } else {
            return res.json({
              meta: {
                page: filter.page,
                count: count,
                limit: filter.limit,
              },
              item: doc,
            })
          }
        })
    })
  } else {
    YNUCompany.count({}, (err, count) => {
      YNUCompany.find(filter.filter)
        .sort(filter.sort)
        .limit(filter.limit)
        .skip((filter.page - 1) * filter.limit)
        .exec((err, doc) => {
          if (filter.file) {
            renderYnu(doc, res)
          } else {
            return res.json({
              meta: {
                page: filter.page,
                count: count,
                limit: filter.limit,
              },
              item: doc,
            })
          }
        })
    })
  }
})
  .get('/:date/', (req, res, next) => {
    const date = dateformat(req.params.date, 'yyyy-mm-dd')
    const filter = req.filter
    Company.find({ meet_day: date })
      .sort(filter.sort)
      .limit(filter.limit)
      .skip((filter.page - 1) * filter.limit)
      .exec((err, doc) => {
        res.json({
          meta: {
            page: filter.page,
            count: doc.length,
            limit: filter.limit,
          },
          item: doc,
        })
      })
  })
module.exports = router
